package com.zjhd.sealcutting.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: 王超
 * @Description:
 * @time: 2020/7/25 17:03
 */
public class ConfigManager {

    private final static Logger logger = LoggerFactory.getLogger(ConfigManager.class);
    private static ConfigManager configManger = new ConfigManager();

    private static final String defaultFileName = "auto";

    private volatile static Map<String, Properties> proMap = new ConcurrentHashMap<String, Properties>();

    private ConfigManager() {

    }

    public static ConfigManager instance() {
        return configManger;
    }

    /**
     *
     * @param profileName
     *            配置文件名（不包括后缀）
     * @param key
     *            属性key值
     * @return
     */
    public String getProperty(String profileName, String key) {
        if (key == null || profileName == null) {
            throw new IllegalArgumentException("key is null");
        }
        Properties properties = proMap.get(profileName);

        if (properties == null) {
            synchronized (this) {

                if (properties == null) {

                    properties = this.loadProps(profileName);

                    if (properties != null) {
                        proMap.put(profileName, properties);
                    } else {
                        return null;
                    }
                }
            }
        }

        String value = properties.getProperty(key);
        return value;
    }
    public void clearProps(String proFileName) {
        proMap.remove(proFileName);
    }
    public String getProperty(String key) {
        return getProperty(defaultFileName,key);
    }
    /**
     *
     * @param profileName
     *            配置文件名（不包括后缀）
     * @param key
     *            属性key值
     * @return
     */
    public int getIntProperty(String profileName, String key) {
        if (key == null || profileName == null) {
            throw new IllegalArgumentException("key is null");
        }

        String intValue = this.getProperty(profileName, key);

        try {
            return Integer.parseInt(intValue);
        } catch (Exception e) {
            //e.printStackTrace();
            logger.error(profileName+"."+key+"="+intValue + " has exeception:"+e.toString());
            return -1;
        }
    }
    public int getIntProperty(String key) {
        return getIntProperty(defaultFileName,key);
    }

    public Properties loadProps(String proFileName) {

        logger.debug("Getting Config");

        Properties properties = null;

        InputStream in = null;

        try {
            properties = new Properties();
            if (proFileName != null && proFileName.startsWith("http:")) {
                URL url = new URL(proFileName);
                in = url.openStream();
            } else {
                String fileName = "/" + proFileName + ".properties";
                in = getClass().getResourceAsStream(fileName);
                properties.load(in);
            }
            properties.load(in);
            logger.info("Successfully  proFileName=" + proFileName + " load Properties:" + properties);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Error reading " + proFileName + " in loadProps() " + e.getMessage());
            logger.error("Ensure the " + proFileName + " file is readable and in your classpath.");
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return properties;
    }
}
